package com.java.permission.common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.java.permission.common.dao.entity.SysOffice;
import com.java.permission.common.dao.entity.SysOfficeUserDetail;
import com.java.permission.common.dao.entity.SysUser;
import com.java.permission.common.dao.mapper.SysOfficeUserDetailMapper;
import com.java.permission.common.exception.ParamsException;
import com.java.permission.common.params.PageParams;
import com.java.permission.common.service.BaseService;
import com.java.permission.common.service.SysOfficeService;
import com.java.permission.common.service.SysOfficeUserDetailService;
import com.java.permission.common.service.SysUserService;
import com.java.permission.common.utils.IdUtils;
import com.java.permission.common.vo.UserIdAndNameVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hu
 * @version 1.0
 * @date 2020/6/15 14:36
 * @description
 */
@Service
public class SysOfficeUserDetailServiceImpl extends BaseService implements SysOfficeUserDetailService {
    @Resource
    private SysOfficeUserDetailMapper sysOfficeUserDetailMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysOfficeService sysOfficeService;

    @Override
    public IPage<SysUser> findPageOfficeUser(PageParams pageParams, String officeId, String userName, String roleId, String realName) {
        return null;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String officeId, List<UserIdAndNameVo> userIdAndNameVoList, String operUserId) {
        //权限校验
        //部门校验
        //用户校验
        SysOffice office = sysOfficeService.findValidById(officeId);
        if (null == office) {
            throw  new ParamsException("当前机构不存在或者已被停用！");
        }
        if (CollectionUtils.isEmpty(userIdAndNameVoList)) {
            throw  new ParamsException("请选择用户！");
        }
        List<String> userIdList = userIdAndNameVoList.stream().map(UserIdAndNameVo::getUserId).distinct().collect(Collectors.toList());
        List<SysUser> sysUserList = sysUserService.findByUserIdIn(userIdList);
        if (CollectionUtils.isEmpty(sysUserList)) {
            throw  new ParamsException("当前选入的学生不存在！");
        }
        List<String> unExistUserNameList = new ArrayList<>();
        LocalDateTime now = LocalDateTime.now();
        for (SysUser sysUser : sysUserList) {
            UserIdAndNameVo tempUser = userIdAndNameVoList.stream().filter(user -> user.getUserId().equals(sysUser.getId())).findFirst().orElse(null);
            if (tempUser == null) {
                unExistUserNameList.add(tempUser.getUserName());
            } else {
                SysOfficeUserDetail sysOfficeUserDetail = new SysOfficeUserDetail();
                sysOfficeUserDetail.setId(IdUtils.newId());
                sysOfficeUserDetail.setUserId(tempUser.getUserId());
                sysOfficeUserDetail.setOfficeId(officeId);
                sysOfficeUserDetail.setCreateAt(now);
                sysOfficeUserDetail.setCreateBy(operUserId);
                sysOfficeUserDetailMapper.insert(sysOfficeUserDetail);
            }
        }

        if (CollectionUtils.isNotEmpty(unExistUserNameList)){
            throw new ParamsException("用户" + StringUtils.join(unExistUserNameList, ",") + "不存在");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id, String operUserId) {
        //todo 校验是否有删除权限
        SysOfficeUserDetail sysOfficeUserDetail = this.findById(id);
        if (sysOfficeUserDetail == null) {
            throw new ParamsException("当前记录不存在！");
        }
        sysOfficeUserDetailMapper.deleteById(id);
    }

    /**
     * 根据id查询关联关系
     * @param id
     * @return
     */
    public SysOfficeUserDetail findById(String id) {
      return   sysOfficeUserDetailMapper.selectById(id);
    }
}
